<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>轮播图</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        list-style: none;
      }
      .banner {
        width: 500px;
        height: 300px;
        border: 1px solid black;
        margin: 0 auto;
        overflow: hidden;
        position: relative;
      }

      ul {
        position: absolute;
        width: 100%;
        height: 100%;
        font-size: 0;
        white-space: nowrap;
        transition: transform 1s;
        transform: translateX(-100%);
      }
      li {
        width: 100%;
        height: 100%;
        display: inline-block;
        font-size: 80px;
        text-align: center;
      }
      .one {
        background-color: pink;
      }
      .two {
        background-color: skyblue;
      }
      .three {
        background-color: green;
      }
      .btn {
        display: flex;
        justify-content: space-between;
      }
      .btn button {
        width: 200px;
      }
    </style>
  </head>
  <body>
    <div class="banner">
      <ul style="left: 0px">
        <li class="three">3</li>
        <li class="one">1</li>
        <li class="two">2</li>
        <li class="three">3</li>
        <li class="one">1</li>
      </ul>
    </div>
    <div class="btn">
      <button class="left">左</button>
      <button class="right">右</button>
    </div>
    <script>
      let ul = document.querySelector("ul");
      let butl = document.querySelector(".left");
      let butr = document.querySelector(".right");
      let index = 1;
      let isClick = false;
      ul.addEventListener("transitionend", function () {
        isClick = false;

        if (index > 3) {
          index = 1;
          ul.style.transitionDuration = "0s";
          ul.style.transform = `translateX(-${index * 100}%)`;
        }
        if (index < 1) {
          index = 3;
          ul.style.transitionDuration = "0s";
          ul.style.transform = `translateX(-${index * 100}%)`;
        }
      });

      butr.addEventListener("click", function () {
        if (isClick) {
          return;
        }
        isClick = true;
        index++;
        ul.style.transitionDuration = "1s";
        ul.style.transform = `translateX(-${index * 100}%)`;
      });
      butl.addEventListener("click", function () {
        if (isClick) {
          return;
        }
        isClick = true;
        index--;
        ul.style.transitionDuration = "1s";
        ul.style.transform = `translateX(-${index * 100}%)`;
      });
    </script>
  </body>
</html>
